package com.mmall.concurrency.example.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import com.mmall.concurrency.annoations.ThreadSafe;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ThreadSafe
public class AtomicExample5 {
	
	/**
	 * 原子性修改一个类的实例的指定的某个变量
	 */
	private static AtomicIntegerFieldUpdater<AtomicExample5> updater 
		= AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class, "count");
	/**
	 * 要更新的变量要是volatile修饰且不是static的变量
	 */
	@Getter
	public volatile int count = 100;
	
	public static void main(String args[]) {
		AtomicExample5 example5 = new AtomicExample5();
		if(updater.compareAndSet(example5, 100, 120)) {
			log.info("update success1,{}",example5.count);			
		}
		
		if(updater.compareAndSet(example5, 100, 120)) {
			log.info("update success2,{}",example5.count);			
		}else {
			log.info("update file2,{}",example5.count);
		}
	}
}
